package com.jml.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class ProducerManage {
    private String producerGroup = "jml_producer";
    private DefaultMQProducer producer;

    public ProducerManage() {
        //示例生产者
        producer = new DefaultMQProducer(producerGroup);
        producer.setVipChannelEnabled(false);
        //绑定name server
        producer.setNamesrvAddr("127.0.0.1:9876");
        start();
    }

    /**
     * 对象在使用之前必须要调用一次，只能初始化一次
     */
    public void start() {
        try {
            this.producer.start();
        } catch (MQClientException e) {
            e.printStackTrace();
        }
    }

    public DefaultMQProducer getProducer() {
        return this.producer;
    }

    /**
     * 一般在应用上下文，使用上下文监听器，进行关闭
     */
    public void shutdown() {
        this.producer.shutdown();
    }
}